package com.project.service.impl;


import com.project.bean.CutPageBean;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;

public class UserServiceImpl extends BaseService implements IUserService {

    @Override
    public void add(UserBean userBean) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.add(userBean);

        session.commit();
        session.close();
    }

    @Override
    public void del(Integer id) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.del(id);

        session.commit();
        session.close();
    }

    @Override
    public void updateGrade(Integer id, String grade) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.updateGrade(id, grade);

        session.commit();
        session.close();
    }

    @Override
    public void updatePwd(Integer id, String passWord) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.updatePwd(id, passWord);

        session.commit();
        session.close();
    }

    @Override
    public UserBean login(String userName, String passWord) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        UserBean userBean = mapper.login(userName, passWord);
        return userBean;
    }

    @Override
    public UserBean findById(Integer id) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        UserBean userBean = mapper.findById(id);
        if (userBean != null) {
            return userBean;
        }
        return null;
    }

    @Override
    public CutPageBean<UserBean> findByItem(Integer pageNO, String userName, LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        CutPageBean<UserBean> cutBean = new CutPageBean<>();
        cutBean.setList(mapper.cutList(userName, startDate, endDate, (pageNO - 1) * CutPageBean.PAGESIZE, CutPageBean.PAGESIZE));
        cutBean.setCount(mapper.cutCount(userName, startDate, endDate));

        session.close();
        return cutBean;
    }
}
